document.addEventListener('DOMContentLoaded', function() {
    const extractBtn = document.getElementById('extractBtn');
    const copyBtn = document.getElementById('copyBtn');
    const clearBtn = document.getElementById('clearBtn');
    const contentText = document.getElementById('contentText');
    const charCount = document.getElementById('charCount');
    const status = document.getElementById('status');

    let currentContent = '';

    // 获取页面内容
    extractBtn.addEventListener('click', async function() {
        try {
            showStatus('正在获取页面内容...', 'info');
            extractBtn.disabled = true;
            extractBtn.textContent = '获取中...';

            // 获取当前活动标签页
            const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });

            // 向 content script 发送消息获取页面内容
            const response = await chrome.tabs.sendMessage(tab.id, { action: 'getInnerText' });

            if (response && response.success) {
                currentContent = response.content;
                displayContent(currentContent);
                showStatus('内容获取成功！', 'success');
            } else {
                throw new Error((response && response.error) || '获取内容失败');
            }
        } catch (error) {
            console.error('获取内容时出错：', error);
            showStatus('获取内容失败：' + error.message, 'error');
        } finally {
            extractBtn.disabled = false;
            extractBtn.textContent = '获取内容';
        }
    });

    // 复制内容到剪贴板
    copyBtn.addEventListener('click', async function() {
        if (!currentContent) {
            showStatus('没有内容可复制', 'error');
            return;
        }

        try {
            await navigator.clipboard.writeText(currentContent);
            showStatus('内容已复制到剪贴板！', 'success');
        } catch (error) {
            console.error('复制失败：', error);
            showStatus('复制失败：' + error.message, 'error');
        }
    });

    // 清空内容
    clearBtn.addEventListener('click', function() {
        currentContent = '';
        contentText.textContent = '点击"获取内容"按钮来提取当前页面的文本内容';
        updateCharCount(0);
        hideStatus();
    });

    // 显示内容
    function displayContent(content) {
        if (!content) {
            contentText.textContent = '页面没有文本内容';
            updateCharCount(0);
            return;
        }

        // 清理和格式化内容
        const cleanContent = content.trim();
        contentText.textContent = cleanContent;
        updateCharCount(cleanContent.length);

        // 滚动到顶部
        contentText.scrollTop = 0;
    }

    // 更新字符计数
    function updateCharCount(count) {
        charCount.textContent = `字符数：${count.toLocaleString()}`;
    }

    // 显示状态消息
    function showStatus(message, type) {
        status.textContent = message;
        status.className = `status ${type}`;
        status.style.display = 'block';

        // 3 秒后自动隐藏
        setTimeout(() => {
            hideStatus();
        }, 3000);
    }

    // 隐藏状态消息
    function hideStatus() {
        status.style.display = 'none';
    }
});